import OrderBar from '../systems/OrderSystem';
import Customer from '../objects/Customer';
import SaveSystem from '../systems/SaveSystem';
import SceneManager from '../systems/SceneManager';

export default class CafeScene extends Phaser.Scene {
    constructor() {
        super('CafeScene');
    }

    create() {
        // 场景基础设置
        this.setupScene();
        
        // 初始化游戏系统
        this.initSystems();
        
        // 设置游戏循环
        this.setupGameLoop();
        
        // 新增互动区域
        this.setupInteractiveZones();

        // 初始化系统
        this.economy = new EconomySystem(this);
        this.orders = new OrderSystem(this);
        this.catInteraction = new CatInteraction(this);

        // 系统间通信
        this.events.on('order_completed', order => {
            this.economy.addGold(order.reward);
        });
        
        // 新增保存系统
        this.saveSystem = new SaveSystem(this);
        
        // 自动保存定时器
        this.time.addEvent({
            delay: 30000, // 每30秒自动保存
            callback: () => this.saveSystem.save(),
            loop: true
        });

        // 初始化成就系统
        this.achievements = new AchievementSystem(this);
        
        // 触发成就检查的示例
        this.events.on('unlock_cat', () => {
            const unlocked = this.registry.get('unlockedCats') || [];
            this.registry.set('unlockedCats', [...unlocked, 'cat_02']);
            this.events.emit('unlock_cat'); // 触发成就检查
        });

        // 初始化场景管理器
        this.sceneManager = new SceneManager(this);
        
        // 添加场景切换按钮
        this.addMenuButton();
    }

    addMenuButton() {
        const menuBtn = this.add.image(100, 100, 'menu_button')
            .setInteractive()
            .on('pointerdown', () => {
                this.sceneManager.openMenu();
            });
    }
}